package test.link;

import lpt.ErrorLpt;

public class DebugLpt extends ErrorLpt {

	private byte lastRead = 34;
	private byte lastSend = 34;
	
	private DebugRegister dbr = DebugRegister.getInstance();
	
	@Override
	/**
	 * Writes the lowest (rightmost) 5 its
	 * of i on the LPT-cable
	 * @param i the int containing the 5 bits to send in the rightmost bits
	 */
	public void writeLPT(int i){
		if(((byte) i) != lastSend){
			dbr.setCableOut((byte) i);
			lastSend = (byte) i;
		}
		super.writeLPT(i);
	}
	
	@Override
	/**
	 * Returns the current bits on the LPT-cable
	 * the significant bits are in the lower (rightmost) 
	 * 5 bits
	 * @return a byte containing the bits on the cable in the lower (rightmost) 5 bits
	 */
	public byte readLPT(){
		byte b = (byte) ((super.readLPT() >> 3) & 0x1F);
		b ^= 0x10; //for some reason this bit always arrives flipped
		if(b != lastRead){
			dbr.setCableIn(b);
			lastRead = b;
		}
		return b;
	}
	
	
	private static void e(String s){
		System.out.println(s);
	}
	
	private static String bin(byte i){
		String x = Integer.toBinaryString(i);
		while(x.length() < 8)
			x = "0" + x;
		return "0bxxx"+(x.substring(x.length()-5, x.length()));
	}
	
	private static String bin(int i){
		String x = Integer.toBinaryString(i);
		while(x.length() < 8)
			x = "0" + x;
		return "0bxxx"+(x.substring(x.length()-5, x.length()));
	}
	
}
